{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chainmap - to search through multiple dicts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {'a': 'A', 'c': 'C'}\n",
    "b = {'b': 'B', 'd': 'D'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = A\n",
      "c = C\n",
      "\n",
      "Keys: ['b', 'c', 'd', 'a']\n",
      "Values: ['B', 'C', 'D', 'A']\n"
     ]
    }
   ],
   "source": [
    "# Accessing\n",
    "\n",
    "import collections\n",
    "\n",
    "dic = collections.ChainMap(a, b)\n",
    "\n",
    "print('a = {}'.format(dic['a']))\n",
    "print('c = {}'.format(dic['c']))\n",
    "print()\n",
    "\n",
    "print('Keys: {}'.format(list(dic.keys())))\n",
    "print('Values: {}'.format(list(dic.values())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D'}\n"
     ]
    }
   ],
   "source": [
    "# A neat trick using unpacking operator introduced in python 3.5\n",
    "\n",
    "z = {**a, **b}\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 'A', 'c': 'C'}, {'b': 'B', 'd': 'D'}]\n",
      "c = C\n",
      "\n",
      "[{'b': 'B', 'd': 'D'}, {'a': 'A', 'c': 'C'}]\n",
      "c = C\n"
     ]
    }
   ],
   "source": [
    "# Reordering\n",
    "\n",
    "print(dic.maps)\n",
    "print('c = {}\\n'.format(dic['c']))\n",
    "\n",
    "dic.maps = list((reversed(dic.maps)))\n",
    "print(dic.maps)\n",
    "print('c = {}'.format(dic['c']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before update: B\n",
      "After update: T\n"
     ]
    }
   ],
   "source": [
    "# Updating values\n",
    "\n",
    "print('Before update: {}'.format(dic['b']))\n",
    "dic['b'] = 'T'\n",
    "print('After update: {}'.format(dic['b']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before dic:  ChainMap({'b': 'T', 'd': 'D'}, {'a': 'A', 'c': 'C'})\n",
      "Before new_ch:  ChainMap({}, {'b': 'T', 'd': 'D'}, {'a': 'A', 'c': 'C'})\n",
      "\n",
      "After dic:  ChainMap({'b': 'T', 'd': 'D'}, {'a': 'A', 'c': 'C'})\n",
      "After new_ch:  ChainMap({'c': 'Y'}, {'b': 'T', 'd': 'D'}, {'a': 'A', 'c': 'C'})\n"
     ]
    }
   ],
   "source": [
    "# Using new child implicitly\n",
    "\n",
    "new_ch = dic.new_child()\n",
    "\n",
    "print('Before dic: ', dic)\n",
    "print('Before new_ch: ', new_ch)\n",
    "print()\n",
    "\n",
    "new_ch['c'] = 'Y'\n",
    "\n",
    "print('After dic: ', dic)\n",
    "print('After new_ch: ', new_ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dic[\"d\"]:  D\n",
      "ch2[\"d\"]:  H\n"
     ]
    }
   ],
   "source": [
    "# Using new child explicitly\n",
    "\n",
    "d = {'d': 'H'}\n",
    "\n",
    "ch2 = dic.new_child(d)\n",
    "\n",
    "print('dic[\"d\"]: ', dic['d'])\n",
    "print('ch2[\"d\"]: ', ch2['d'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
